package com.nexgo.payment.pack;

import com.nexgo.common.ByteUtils;
import com.nexgo.data.entity.SendData;
import com.nexgo.data.entity.SendDataBuilder;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.TransType;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.databases.TransRecord;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.util.MessageHelper;

/**
 * Created by xiaox on 2017/8/16.
 */

class ReversalBuilder extends DataBuilder {

    private TransType mOrgTransType;
    private TransRecord mTransRecord;

    ReversalBuilder(TransData transData, DataSource repository) {
        super(transData, repository);
        mTransRecord = mRepository.queryTransRecord(mTransData.getOrgTraceNum());
        mOrgTransType = TransType.values()[mTransRecord.getTransType()];
    }

    @Override
    SendData form() {
        SendData sendData = new SendDataBuilder().setListener(mListener).setSignListener(mSignListener).createSendData();
        sendData.setHeader(MessageHelper.formMessageHeader(mRepository.getParamValue(PreferencesConstants.TPDU)));
        sendData.setMsgType(mTransData.getTransType().getMsgType());
        sendData.setPan(mTransRecord.getPan());
        sendData.setProcCode(mOrgTransType.getProcCode());
        sendData.setAmount(mTransRecord.getAmount());
//        wzp add
        sendData.setTraceNum(mRepository.getParamValue(PreferencesConstants.TRACE_NUMBER));
        setCurruntTraceNumber(mRepository.getParamValue(PreferencesConstants.TRACE_NUMBER));
        mRepository.increaseTraceNumber();

//        sendData.setTraceNum(mTransRecord.getTraceNum());
        sendData.setExpDate(mTransRecord.getExpDate());
        sendData.setEntryMode(mTransRecord.getEntryMode());
        sendData.setCsn(mTransRecord.getCsn());
        sendData.setConditionCode(mOrgTransType.getConditionCode());
//        sendData.setAuthCode(mTransRecord.getAuthCode());
//        sendData.setRespCode(mTransRecord.getReversalCode());
//       上送原交易35  可以不送
//        sendData.setTrack2(mOrgTransType.);
        sendData.setTerminalId(mTransRecord.getTerminalId());
        sendData.setMerchantId(mTransRecord.getMerchantId());
        sendData.setFundType(mTransRecord.getFundType());
        sendData.setEmvTlv(mTransRecord.getReversalTlv());
/*        //扫码交易冲正
        if (TransType.SCAN_SALE == mOrgTransType || TransType.SCAN_VOID == mOrgTransType){
            sendData.setField59(formField59());
        }*/
//        获取原交易60 62
        sendData.setField60(mTransRecord.getField60());
        sendData.setField62(formField62());
 /*       if(!(TransType.SCAN_SALE == mOrgTransType
                || TransType.SALE == mOrgTransType
                || TransType.AUTH == mOrgTransType
                || TransType.EC_SALE == mOrgTransType)) {
            sendData.setField61(formField61());
        }*/
        sendData.setMac("0000000000000000");
        return sendData;
    }


    /*   private String formField59(){
           StringBuilder sb = new StringBuilder();
           String payCodeNo = mTransRecord.getPayCodeNo();
           if (!TextUtils.isEmpty(payCodeNo)){
               sb.append("A3");
               sb.append(new Formatter().format("%03d", payCodeNo.length()).toString());
               sb.append(payCodeNo);
           }
           return ByteUtils.byteArray2HexString(sb.toString().getBytes());
       }
   */
    private String formField60() {
        StringBuilder sb = new StringBuilder();
        sb.append(mOrgTransType.getFuncCode());
        sb.append(mTransRecord.getBatchNum());
        return ByteUtils.byteArray2HexString(sb.toString().getBytes());
    }

    private String formField62() {
        StringBuilder sb = new StringBuilder();
        sb.append(mTransRecord.getBatchNum()); // 批次
        sb.append(mTransRecord.getTraceNum()); //流水
        if(TransType.AUTH_VOID == mOrgTransType
                || TransType.VOID == mOrgTransType
                || TransType.AUTH_CM_VOID == mOrgTransType) {
            sb.append(mTransRecord.getOrgTraceNum()); //原流水号
        }
        return sb.toString();
    }
}
